package com.zjw.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
public class AppResult<T> {
    @JsonInclude(JsonInclude.Include.ALWAYS)//必须序列化
    private Integer code;
    @JsonInclude(JsonInclude.Include.ALWAYS)//必须序列化
    private String message;
    @JsonInclude(JsonInclude.Include.ALWAYS)//必须序列化
    private T data;

    //写构造方法 给静态方法提供服务的 静态方法返回对象是会调用构造方法
    public AppResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public AppResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    //SUCCESS静态方法 code是不需要改变的
    //1.直接调用，没有data
    public static <T> AppResult<T> success(){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }
    //2.有data
    public static <T> AppResult<T> success(T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }
    //3.message自定义且没有data 比如：登陆成功、添加成功什么的
    public static <T> AppResult<T> success(String message){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),message);
    }
    //4.message自定义且有data
    public static <T> AppResult<T> success(String message,T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(),message,data);
    }

    //failed静态方法 失败是没有data的
    //1.直接调用，没有data
    public static <T> AppResult<T> failed(){
        return new AppResult<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }
    //2.(使用FAILED的状态码，但是具体的message调用者传入)
    public static <T> AppResult<T> failed(String message){
        return new AppResult<>(ResultCode.FAILED.getCode(),message);
    }
    //3.(使用枚举类中的其他失败对象)
    public static <T> AppResult<T> failed(ResultCode resultCode){
        return new AppResult<>(resultCode.getCode(),resultCode.getMessage());
    }

}

